﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 数据查询
    /// </summary>
    [Serializable]
    public partial class DataQuery : AbsMetaDataContent, ISchema
    {
        /// <summary>
        /// 主键id
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 数据查询标识
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 数据查询名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 0:列表查询，1:树查询
        /// </summary>
        public QueryType QueryType { get; set; }

        /// <summary>
        /// 0:返回顶级列表，1:返回含有子节点的顶级列表
        /// </summary>
        public TreeListType TreeListType { get; set; }

        /// <summary>
        /// 超时时间(单位：秒）
        /// </summary>
        public int Timeout { get; set; }

        /// <summary>
        /// 缓存时间(单位：分钟）
        /// </summary>
        public int CacheInMinutes { get; set; }

        /// <summary>
        /// 是否启用权限
        /// </summary>
        public bool IsPermissionControl { get; set; }

        /// <summary>
        /// 是否启用缓存
        /// </summary>
        public bool IsCache { get; set; } 

        /// <summary>
        /// 是否启用缓存
        /// </summary>
        public bool IsDistinct { get; set; }

        /*******************************/
        /// <summary>
        /// 是否启用掩码
        /// </summary>
        public bool IsEnableMask { get; set; }

        /// <summary>
        /// 租户id
        /// </summary>
        public string TenantID { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        public string Ext1 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        public string Ext2 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        public string Ext3 { get; set; }

        /// <summary>
        /// Action权限控制设置信息,ToDo
        /// </summary>
        //public ActionPermissionDefinitionForView ActionPermission { get; set; }

        /// <summary>
        /// 数据对象集合
        /// </summary>
        public List<DataQuerySource> DataQuerySources { get; set; }

        /// <summary>
        /// 数据查询列集合
        /// </summary>
        public List<DataQueryColumn> DataQueryColumns { get; set; }

        /// <summary>
        /// 数据查询join关联关系集合
        /// </summary>
        public List<DataQueryJoin> DataQueryJoins { get; set; }

        /// <summary>
        /// 数据查询过滤条件集合
        /// </summary>
        public List<DataQueryFilter> DataQueryFilters { get; set; }

        /// <summary>
        /// 数据查询排序条件集合
        /// </summary>
        public List<DataQuerySort> DataQuerySorts { get; set; }

        /// <summary>
        /// 数据查询变量集合
        /// </summary>
        public List<DataQueryParam> DataQueryParams { get; set; }

        /// <summary>
        /// 子查询集合
        /// </summary>
        public List<DataQuerySubQuery> SubQuerys { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public override string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        public override DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        public override string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        public override DateTime LastModifyTime { get; set; }

        /// <summary>
        /// 特供CDO下的DQ使用，用于记录COD id，建立弱依赖关系
        /// </summary>
        public string RelatedMetaDataID { get; set; }

        public override object Clone()
        {
            var newDQ = this.MemberwiseClone() as DataQuery;

            if (DataQuerySources != null)
            {
                newDQ.DataQuerySources = new List<DataQuerySource>();
                this.DataQuerySources.ForEach(i => newDQ.DataQuerySources.Add(i.Clone() as DataQuerySource));
            }

            if (DataQueryColumns != null)
            {
                newDQ.DataQueryColumns = new List<DataQueryColumn>();
                this.DataQueryColumns.ForEach(i => newDQ.DataQueryColumns.Add(i.Clone() as DataQueryColumn));
            }

            if (DataQueryJoins != null)
            {
                newDQ.DataQueryJoins = new List<DataQueryJoin>();
                this.DataQueryJoins.ForEach(i => newDQ.DataQueryJoins.Add(i.Clone() as DataQueryJoin));
            }

            if (DataQueryFilters != null)
            {
                newDQ.DataQueryFilters = new List<DataQueryFilter>();
                this.DataQueryFilters.ForEach(i => newDQ.DataQueryFilters.Add(i.Clone() as DataQueryFilter));
            }

            if (DataQuerySorts != null)
            {
                newDQ.DataQuerySorts = new List<DataQuerySort>();
                this.DataQuerySorts.ForEach(i => newDQ.DataQuerySorts.Add(i.Clone() as DataQuerySort));
            }

            if (DataQueryParams != null)
            {
                newDQ.DataQueryParams = new List<DataQueryParam>();
                this.DataQueryParams.ForEach(i => newDQ.DataQueryParams.Add(i.Clone() as DataQueryParam));
            }

            return newDQ;
        }

        /// <summary>
        /// 获取DQ的MainSource的主键列，方便取出主键列Alias
        /// </summary>
        /// <returns></returns>
        public DataQueryColumn GetMainSourcePkColumn()
        {
            DataQuerySource mainSource = this.DataQuerySources.FirstOrDefault(s => s.IsMainSource);
            var pkColID = mainSource.DataObject?.Columns.FirstOrDefault(col => col.IsPkColumn).ID;
            //if (string.IsNullOrEmpty(pkColID))
            //    pkColID = mainSource.DataAsset?.Columns.FirstOrDefault(col => col.IsPkColumn).ID;
            var pkCol = this.DataQueryColumns.FirstOrDefault(col => col.DataQuerySourceID == mainSource.ID && col.DataObjectColumnID == pkColID);
            return pkCol;
        }

        public bool IsBigDataQuerySource()
        {
            var mainSource = DataQuerySources.FirstOrDefault(s => s.IsMainSource);
            if (mainSource == null) return false;
            return mainSource.SourceType == DataQuerySourceType.DataAsset;
        }
    }

    /// <summary>
    /// DQ查询类型，返回List或TreeList
    /// </summary>
    public enum QueryType
    {
        List,
        TreeList,
    }

    /// <summary>
    /// TreeList返回类型
    /// </summary>
    public enum TreeListType
    {
        OnlyTopLevel,
        OnlyExistChilds,
    }
}
